package com.uchonor.api.core.controller;

import com.uchonor.api.core.service.DataSourceMetaDataContext;
import com.uchonor.api.core.service.IDataSourceMetaDataStrategy;
import com.uchonor.api.utils.ResponseUtils;
import com.uchonor.olight.agent.annotation.Inject;
import com.uchonor.olight.agent.annotation.Server;
import com.uchonor.olight.agent.annotation.web.Get;
import com.uchonor.olight.agent.annotation.web.Post;
import com.uchonor.olight.agent.module.web.vo.ServiceResult;
import com.uchonor.olight.agent.module.web.vo.StatusCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.ssssssss.magicapi.core.config.MagicConfiguration;
import org.ssssssss.magicapi.core.service.MagicDynamicRegistry;
import org.ssssssss.magicapi.datasource.model.DataSourceInfo;
import org.ssssssss.magicapi.datasource.model.MagicDynamicDataSource;
import org.ssssssss.magicapi.datasource.service.DataSourceMagicDynamicRegistry;
import org.ssssssss.magicapi.utils.JdbcUtils;

import java.sql.Connection;
import java.util.*;

@Server(path = "/database")
public class DatabaseController {
    @Inject
    private MagicConfiguration configuration;

    @Inject
    private MagicDynamicDataSource magicDynamicDataSource;

    @Inject
    private DataSourceMetaDataContext dataSourceMetaDataContext;

    @Post(path = "/jdbc/test")
    public ServiceResult jdbcTest(@RequestBody DataSourceInfo properties) {
        ServiceResult serviceResult = new ServiceResult();
        try {
            Connection connection = JdbcUtils.getConnection(properties.getDriverClassName(), properties.getUrl(), properties.getUsername(), properties.getPassword());
            JdbcUtils.close(connection);
            serviceResult.setRSP(StatusCode.DEFAULT.getKey(), "操作成功", null);
        } catch (Exception e) {
            serviceResult = ResponseUtils.getExceptionResult(e);
        }
        return serviceResult;
    }

    @Get(path = "/getDatabaseList")
    public ServiceResult getDatabaseList() {
        ServiceResult serviceResult = new ServiceResult();
        try {
            Set<DataSourceInfo> resultList = new LinkedHashSet<>();
            MagicDynamicRegistry magicDynamicRegistry = configuration.getMagicDynamicRegistries().stream().filter(item -> item instanceof DataSourceMagicDynamicRegistry).findFirst().orElse(null);
            if (magicDynamicRegistry != null) {
                List<DataSourceInfo> dataSourceInfoList = ((DataSourceMagicDynamicRegistry) magicDynamicRegistry).mappings();
                resultList.addAll(dataSourceInfoList);
            }
            serviceResult.setRSP(StatusCode.DEFAULT.getKey(), "操作成功", resultList);
        } catch (Exception e) {
            serviceResult = ResponseUtils.getExceptionResult(e);
        }
        return serviceResult;
    }

    @Get(path = "/getAllTableInfo")
    public ServiceResult getAllTable(String dataBaseKey) {
        ServiceResult serviceResult = new ServiceResult();
        Connection connection = null;
        try {
            if (StringUtils.isNotBlank(dataBaseKey)) {
                MagicDynamicDataSource.DataSourceNode dataSourceNode = magicDynamicDataSource.getDataSource(dataBaseKey);
                if (dataSourceNode != null) {
                    connection = dataSourceNode.getDataSource().getConnection();
                    String catalog = connection.getCatalog();
                    IDataSourceMetaDataStrategy iDataSourceMetaDataStrategy = dataSourceMetaDataContext.getDataSourceMetaDataStrategy(connection.getMetaData().getDriverName().toUpperCase());
                    serviceResult.setRSP(StatusCode.DEFAULT.getKey(), "操作成功", iDataSourceMetaDataStrategy.getAllTableList(dataSourceNode.getJdbcTemplate(), catalog));
                }
            }
        } catch (Exception e) {
            serviceResult = ResponseUtils.getExceptionResult(e);
        } finally {
            JdbcUtils.close(connection);
        }
        return serviceResult;
    }

    @Get(path = "/getColumnsInfo")
    public ServiceResult getColumnsInfo(String dataBaseKey, String tableName) {
        ServiceResult serviceResult = new ServiceResult();
        Connection connection = null;
        try {
            if (StringUtils.isNotBlank(dataBaseKey)) {
                MagicDynamicDataSource.DataSourceNode dataSourceNode = magicDynamicDataSource.getDataSource(dataBaseKey);
                if (dataSourceNode != null) {
                    connection = dataSourceNode.getDataSource().getConnection();
                    String catalog = connection.getCatalog();
                    IDataSourceMetaDataStrategy iDataSourceMetaDataStrategy = dataSourceMetaDataContext.getDataSourceMetaDataStrategy(connection.getMetaData().getDriverName().toUpperCase());
                    serviceResult.setRSP(StatusCode.DEFAULT.getKey(), "操作成功", iDataSourceMetaDataStrategy.getColumnsInfo(dataSourceNode.getJdbcTemplate(), catalog, tableName));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            serviceResult = ResponseUtils.getExceptionResult(e);
        } finally {
            JdbcUtils.close(connection);
        }
        return serviceResult;
    }
}
